package com.ruoyi.web.controller.hbs;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.hbs.domain.SalesInventoryRecords;
import com.ruoyi.hbs.service.SalesInventoryRecordsService;
import com.ruoyi.hbs.service.SingleSalesInventoryRecordsService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.util.Date;
import java.util.List;

/**
 * 码上健康-药品销售与库存Controller
 *
 * @author wl
 * @date 2025年11月04日13:37
 */
@RestController
@RequestMapping("/hbs/salesInventoryRecords")
@Api(tags = "销售库存管理")
@Slf4j
public class SalesInventoryRecordsController extends BaseController {

    @Autowired
    private SalesInventoryRecordsService salesInventoryRecordsService;
    @Autowired
    private SingleSalesInventoryRecordsService singleSalesInventoryRecordsService;

    /**
     * 查询码上健康-药品销售与库存列表
     */
    @GetMapping("/list")
    public TableDataInfo list(SalesInventoryRecords salesInventoryRecords) {
        startPage();
        List<SalesInventoryRecords> list = salesInventoryRecordsService.selectSalesInventoryRecordsList(salesInventoryRecords);
        return getDataTable(list);
    }

    @Log(title = "销售库存管理导入", businessType = BusinessType.IMPORT)
    @ApiOperation("销售库存管理导入")
    @PostMapping("/importData")
    public AjaxResult importData(MultipartFile file) throws Exception {
        ExcelUtil<SalesInventoryRecords> util = new ExcelUtil<>(SalesInventoryRecords.class);
        List<SalesInventoryRecords> salesInventoryRecordsList = util.importExcel(file.getInputStream());
        String operName = getUsername();
        String message = salesInventoryRecordsService.importSalesInventoryRecords(salesInventoryRecordsList, operName);
        return AjaxResult.success(message);
    }

    /**
     * msfx- 简化药品销售与库存记录表入库
     */
    @ApiOperation(value = "简化药品销售与库存记录表入库", notes = "执行简化药品销售与库存记录表入库")
    @GetMapping(value = "/msfxProcessTerminalFirstOrder")
    @Log(title = "msfx-简化药品销售与库存记录表入库", businessType = BusinessType.COLLECT)
    public AjaxResult msfxProcessTerminalFirstOrder() {
        try {
            log.info("开始执行msfx-简化药品销售与库存记录表");
            singleSalesInventoryRecordsService.syncImport();
            log.info("结束执行msfx-简化药品销售与库存记录表");
        } catch (Exception e) {
           return AjaxResult.error("处理简化药品销售与库存记录表入库异常"+ e);
        }
        return AjaxResult.success();
    }


    /**
     * HBS- 确认处理伙伴云映射表和终端首单数据
     */
    @ApiOperation(value = "执行数据匹配任务和处理终端首单记录", notes = "处理未匹配的记录和处理终端首单记录")
    @GetMapping(value = "/dataMatchAndProcessTerminalFirstOrder")
    @Log(title = "HBS-处理伙伴云映射表和终端首单数", businessType = BusinessType.COLLECT)
    public AjaxResult dataMatchAndProcessTerminalFirstOrder() {
        try {
            long startTime = System.currentTimeMillis();
            log.info("开始执行数据匹配定时任务，开始时间: {}", new Date(startTime));
            singleSalesInventoryRecordsService.processUnmatchedRecords();
            long endTime = System.currentTimeMillis();
            log.info("数据匹配定时任务执行完成，结束时间: {}，耗时: {}ms", new Date(endTime), endTime - startTime);
            log.info("开始去除重复的商业映射和终端映射");
            singleSalesInventoryRecordsService.deleRepeat();
            log.info("结束去除重复的商业映射和终端映射");
            log.info("开始处理终端首单记录");
            singleSalesInventoryRecordsService.msfxProcessTerminalFirstOrder();
            log.info("结束处理终端首单记录");
        } catch (Exception e) {
            return AjaxResult.error("处理未匹配的记录和处理终端首单记录数据处理异常"+ e);
        }
        return AjaxResult.success();
    }


    /**
     * TODO 测试
     * msfx- 实验处理终端首单记录和中间表映射数据
     */
    @ApiOperation(value = "处理终端首单记录", notes = "执行并处理终端首单记录")
    @GetMapping(value = "/processTerminalFirstOrder")
    @Log(title = "msfx-处理终端首单记录", businessType = BusinessType.COLLECT)
    public AjaxResult processTerminalFirstOrder() {
        try {
//        去除重复的商业映射和终端映射
            log.info("开始去除重复的商业映射和终端映射");
            singleSalesInventoryRecordsService.deleRepeat();
            log.info("结束去除重复的商业映射和终端映射");
            log.info("开始处理msfx-终端首单记录");
            singleSalesInventoryRecordsService.msfxProcessTerminalFirstOrder();
            log.info("结束处理msfx-终端首单记录");
        } catch (Exception e) {
            return AjaxResult.error("处理终端首单记录和中间表映射数据处理异常"+ e);
        }
        return AjaxResult.success();
    }
}
